//package smartt.styy.auth.advice;
//
//import java.security.interfaces.RSAPrivateKey;
//
//import org.apache.commons.codec.binary.Base64;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//
//import lombok.extern.slf4j.Slf4j;
//import smartt.styy.auth.annotation.SerializedField;
//import smartt.styy.auth.util.RSAEncrypt;
//
//
///**
// * 返回数据加密
// */
//@SuppressWarnings("rawtypes")
//@ControllerAdvice(basePackages = "smartt.styy.auth.controller")
//@Slf4j
//public class MyResponseBodyAdvice implements ResponseBodyAdvice {
//
//	@Value("${ras-keystore.privateKey}")
//	private String privateKey;
//	
//	@Override
//	public boolean supports(MethodParameter returnType, Class converterType) {
//		/*支持所有方法*/
//		return true;
//	}
//
//	@Override
//	public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
//			Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
//		boolean encode = false;
//		//加密
//        log.info("【接受到的数据】encode={}",body);
//		if (returnType.getMethod().isAnnotationPresent(SerializedField.class)) {
//            //获取注解配置的包含和去除字段
//            SerializedField serializedField = returnType.getMethodAnnotation(SerializedField.class);
//            //是否加密
//            encode = serializedField.encode();
//        }
//        if (encode) {
//            log.info("对方法method :" + returnType.getMethod().getName() + "返回数据进行加密");
//            ObjectMapper objectMapper = new ObjectMapper();
//            try {
//                String result = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(body);
//                
//                // 私钥加密过程
//                RSAPrivateKey rsaPrivateKey = RSAEncrypt.loadPrivateKeyByStr(privateKey);
//                byte[] cipherData = RSAEncrypt.encrypt(rsaPrivateKey, result.getBytes());
//                Base64 base64 = new Base64();
//                String cipher = new String(base64.encode(cipherData));
//                log.info("私钥加密后的返回："+cipher);
//                return cipher;
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        
//        return body;
//	}
//
//}
